package com.dengyafei.result;


import com.dengyafei.exception.BaseException;

import java.util.HashMap;
import java.util.UUID;

/**
 * @author 邓亚非
 */
public class RestApiResult extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public RestApiResult() {
        put("code", "000000");
        put("desc", "success");
        put("logId", UUID.randomUUID().toString());
    }

    public RestApiResult(Exception e) {
        if (e instanceof BaseException) {
            this.put("code", ((BaseException) e).getCode());
            this.put("desc", e.getMessage());
        } else {
            this.put("code", 99999);
            this.put("desc", e.getMessage());
        }
    }

    public static RestApiResult error(int code, String msg) {
        RestApiResult r = new RestApiResult();
        r.put("code", code);
        r.put("desc", msg);
        return r;
    }

    public static RestApiResult error(String msg) {
        RestApiResult result = new RestApiResult();
        result.put("desc", msg);
        return result;
    }

    public static RestApiResult ok(String code, String msg) {
        RestApiResult r = new RestApiResult();
        r.put("code", code);
        r.put("desc", msg);
        return r;
    }

    public static RestApiResult error(String code, String msg) {
        RestApiResult r = new RestApiResult();
        r.put("code", code);
        r.put("desc", msg);
        return r;
    }

    public static RestApiResult okMsg(String msg) {
        RestApiResult r = new RestApiResult();
        r.put("desc", msg);
        return r;
    }


    public static RestApiResult ok(Object o) {
        RestApiResult r = new RestApiResult();
        r.put("result", o);
        return r;
    }

    public static RestApiResult ok() {
        return new RestApiResult();
    }


    @Override
    public RestApiResult put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
